<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//
require_once("Gecko/Form/Exception.php");
require_once("Gecko/Form/Field.php");

/**
 * Time Control for selecting hours and minutes
 *
 * @package Gecko.Form.Field;
 * @author Christopher Valderrama <valderrama.christopher@gmail.com>
 * @copyright Copyright (c) 2008
 * @version $Id$v1.0$ 2008
 * @access public
 **/
class Gecko_Form_Field_Time extends Gecko_Form_Field {
	/**
	 * The Hour Range for the Hour Select
	 *
	 * @var array
	 */
	private $hourRange = array();

	/**
	 * The Minutes Range for the Minute Select
	 *
	 * @var array
	 */
	private $timeRange = array();

	/**
	 * The Type of the field
	 *
	 * @var string
	 */
	protected $type = 'time';

	/**
	 * Creates a new Time Control for selecting ranges of hours and minutes
	 *
	 * @param string $name
	 * @param string_type $value
	 * @param array $params
	 * @access public
	 */
	public function __construct($name, $value = "", $params = array() ) {
		parent::__construct( $name, $value, $params );

		$hourRange = ( isset( $params['hourRange'] ) ? $params['hourRange'] : null );
		$timeRange = ( isset( $params['timeRange'] ) ? $params['timeRange'] : null );

		if(!$hourRange) {
			$hourRange = range( 0, 24 );
		}

		if(!$minuteRange) {
			$minuteRange = range( 1, 60 );
		}

		$this->hourRange = $hourRange;
		$this->timeRange = $timeRange;
	}

	/**
	 * Gets the control Value
	 *
	 * @return string
	 */
	public function getValue() {
		$val = parent::getValue();

		$hour = $val['Hours'];
		$minute = $val['Minutes'];

		return sprintf( "%02d:%02d" , $hour, $minute );
	}

	/**
	 * Sets the control value
	 *
	 * @param string $value
	 */
	public function setValue($value) {
		$raw = explode(":", $value);
		$time = array("Hours" => $raw[0], "Minutes" => $raw[1]);

		parent::setValue($time);
	}

	/**
	 * Gets the RAW value (The inner array)
	 *
	 * @return array
	 */
	public function getRawValue() {
		return parent::getValue();
	}

	/**
	 * Gets the HTML Control
	 *
	 * @return string
	 */
	public function getControl() {
		$params = $this->getParams();
		$value = $this->getValue();
		$name = $this->getName();
		$vars = array();
		$template = Gecko_Router::LIBRARY_DIR . "/Assets/Form/Templates/Select.php";

		/** Hour Select **/
		$data = array();
		foreach( $this->hourRange as $hour ) {
			$data[$hour] = sprintf( "%02d", $hour );
		}
		$vars['data'] = $data;
		$vars['name'] = $name . "[Hours]";
		$vars['value'] = $value['Hours'];
		$vars['emptyLabel'] = $params['emptyLabel'];

		$sel1 = Gecko_Template::renderTemplate( $template, $vars, true );

		/** Minute Select **/
		$data = array();
		foreach( $this->timeRange as $minute ) {
			$data[$minute] = sprintf( "%02d", $minute );
		}
		$vars['data'] = $data;
		$vars['name'] = $name . "[Minutes]";
		$vars['value'] = $value['Minutes'];
		$vars['emptyLabel'] = $params['emptyLabel'];

		$sel2 = Gecko_Template::renderTemplate( $template, $vars, true );

		return $sel1 . "&nbsp;" . $sel2;
	}
}